var TableCommon = function () {
    return {
        init: function () {
			
			/* Formatting function for row details */
			/* function fnFormatDetails ( oTable, nTr )
			{
				var aData = oTable.fnGetData( nTr );
				var sOut = '<table>';
				sOut += '<tr><td><img src="'+Upload+'/'+aData['managerPhoto'].replace(".","_m.")+'" alt="" class="img-responsive"></td><td>';
				sOut += '<p>用户名：'+aData['loginName']+'</p>';
				sOut += '<p>编号：'+aData['managerName']+'</p>';
				sOut += '<p>用户名：'+aData['namagerNo']+'</p>';
				sOut += '<p>角色：'+aData['roleName']+'</p>';
				sOut += '<p>最后登录时间：'+aData['lastTime']+'</p>';
				sOut += '<p>最后登录IP：'+aData['lastIP']+'</p>';
				sOut += '<p>账户创建时间：'+aData['createTime']+'</p>';
				sOut += '</td></tr></table>';				 
				return sOut;
			} */
          
            oTable = $('#table_common').dataTable({
                "aLengthMenu": [
                    [-1,5, 15, 20],
                    ["全部",'5条', '15条', '20条'] 
                ],
                "iDisplayLength": -1,
				"serverSide":true,
				"aaSorting":[[6,'desc']],
                "sPaginationType": "bootstrap",
				//"bStateSave": true, 
                "oLanguage": {
					"sSearch":"搜索",
                    "sLengthMenu": "每页显示 _MENU_ 记录",    
					"sEmptyTable":"没有数据",
					"sInfo": "当前数据为从第 _START_ 到第 _END_ 条数据；总共有 _TOTAL_ 条记录",   
					"sInfoEmpty": "没有数据",   
					"sInfoFiltered" : "",
					"sInfoPostFix" : "",
					"sLoadingRecords":"",
					"sProcessing": "正在加载数据...", 
					"sZeroRecords": "没有检索到数据", 
                    "oPaginate": {   
						"sFirst": "首页",   
						"sPrevious": "上一页",   
						"sNext": "下一页",   
						"sLast": "尾页"  
					}   
                },
				/* "sAjaxSource":ajaxUrl,
				"sAjaxDataProp": "data",
				
				"aoColumns": [
					{"sTitle":'',"mDataProp": 'managerId'},
					{"sTitle":'头像',"mDataProp": 'managerPhoto'},
					{"sTitle":'账号',"mDataProp": 'loginName'},
					{"sTitle":'姓名',"mDataProp": 'managerName'},
					{"sTitle":'编号',"mDataProp": 'managerNo'},
					{"sTitle":'角色',"mDataProp": 'managerRoleId'},
					{"sTitle":'最后登录时间',"mDataProp": 'lastTime'},
					{"sTitle":'最后登录IP',"mDataProp": 'lastIP'},
					{"sTitle":'是否在职',"mDataProp": 'workStatus'},
					{"sTitle":'是否启用',"mDataProp": 'managerStatus'},
					{"sTitle":'操作',"mDataProp": 'createTime'}
				],
                "aoColumnDefs": [{
                        'bSortable': false,
						'aTargets': ['_all']
                    },
					
					{"aTargets":[1],"mRender":function(data,type,full){
                     return '<img src="'+Upload+'/'+data.replace(".","_s.")+'"/>';
                    },"sClass":'text-center',"sWidth":'5%'},
					
					{"aTargets":[5],"mRender":function(data,type,full){
                     return full['roleName'];
                    },"sClass":'text-center',"sWidth":'5%'},
					
					{"aTargets":[8],"mRender":function(data,type,full){
						if(full['managerId']!=1 && full['managerId']!=managerId){
							if(data==1){							
								return '<a href="#" class="workStatus btn btn-xs blue">在职</a>';
							}else{
								return '<a href="#" class="workStatus btn btn-xs default">离职</a>';
							}
						}else{
							return '';
						}
                    },"sClass":'text-center',"sWidth":'5%'},
					
					{"aTargets":[9],"mRender":function(data,type,full){
						if(full['managerId']!=1 && full['managerId']!=managerId){
							if(data==1){
								
								return '<a href="#" class="managerStatus btn btn-xs blue">启用</a>';
								
							}else{
								return '<a href="#" class="managerStatus btn btn-xs default">停用</a>';
							}
						}else{
							return '';
						}
                    },"sClass":'text-center',"sWidth":'5%'},
					
					{"aTargets":[10],"mRender":function(data,type,full){
						if(full['managerId']!=1 && full['managerId']!=managerId){
							return '<a class="edit btn btn-xs default" href=""><i class="fa fa-edit"></i> 修改</a><a class="delete btn btn-xs red" href=""><i class="fa fa-trash-o"></i> 删除</a>';
						}else{
							return '<a class="edit btn btn-xs default" href=""><i class="fa fa-edit"></i> 修改</a>';
						}
                    },"sClass":'text-center',"sWidth":'5%'},
					{"aTargets":[0],"mRender":function(data,type,full){
						return "<span class='row-details row-details-close' data_id='"+data['managerId']+"'></span>";
                    },"sClass":'text-center',"sWidth":'5%'}
					
                ], */
				
				
            });

            jQuery('#table_common_wrapper .dataTables_filter input').addClass("form-control input-medium input-inline"); // modify table search input
            jQuery('#table_common_wrapper .dataTables_length select').addClass("form-control input-small"); // modify table per page dropdown
            jQuery('#table_common_wrapper .dataTables_length select').select2({
                showSearchInput : false 
            }); 
			
			var nEditing = null;

			$('#table_common').on('click', ' tbody td .row-details', function () {
				var nTr = $(this).parents('tr')[0];
				if ( oTable.fnIsOpen(nTr) ){
					/* This row is already open - close it */
					$(this).addClass("row-details-close").removeClass("row-details-open");
					oTable.fnClose( nTr );
				}else{
					/* Open this row */                
					$(this).addClass("row-details-open").removeClass("row-details-close");
					oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
				}
			});
			
			//添加
            $('#table_common_new').click(function (e) {
				
                //$('#responsive form').attr('class','manager-form');//切换表单
				$('.modal-header h4')[0].innerHTML = '添加新职员';
				
				var jqName = $('#responsive input[name="loginName"]').eq(0);//清空角色名
				jqName.val('');
				jqName.attr('disabled',false);//解除禁用
				var jqAjax = $('#responsive input[name="noAjax"]').eq(0);
				jqAjax.val('addManagers');//切换提交地址
				var jqRole = $('#responsive input[name="managerId"]').eq(0);
				jqRole.val('');//清空ID
				//$('#responsive button[type="submit"]').eq(0).removeClass('edit');//移除修改按钮
				$('.fileinput img').attr('src',Upload+'/avatar/avatar_m.jpg');
				$('#responsive input').val(""); 
				/*重置选择表单*/
				$('#responsive input[type="checkbox"]').attr('disabled',false);
				$('.checkbox-inline div').removeClass('disabled');
				
				$('#responsive input[type="checkbox"]').attr('checked',true);
				$('#responsive input[type="checkbox"]').parent('span').addClass('checked');
				//select
				$('option').attr('selected',false);
				$('#responsive').modal('show');//显示模态框 
			$("#loginPwd").rules('add',{required:true});
				$("#reLoginPwd").rules('add',{required:true});
				
            });
			
			//在职状态
			$('#table_common a.workStatus').live('click', function (e) {
                e.preventDefault();
                var nRow = $(this).parents('tr')[0];
				var aData = oTable.fnGetData(nRow);
				var workStatus = aData['workStatus']==1?0:1;
				$.post(ajaxUrl,{managerId:aData['managerId'],workStatus:workStatus,ajax:'workStatus'},function(data){
					if(data){
						layer.msg('在职状态修改成功！',{icon: 1,time: 2000});
						oTable.fnUpdate(workStatus, nRow,8, false);
						oTable.fnDraw();
					}else{
						layer.msg('在职状态未修改！',{icon: 2,time: 2000});
						return;
					}
			   });
            });
			
			//账户状态
			$('#table_common a.managerStatus').live('click', function (e) {
                e.preventDefault();
                var nRow = $(this).parents('tr')[0];
				var aData = oTable.fnGetData(nRow);
				var managerStatus = aData['managerStatus']==1?0:1;
				$.post(ajaxUrl,{managerId:aData['managerId'],managerStatus:managerStatus,ajax:'managerStatus'},function(data){
					if(data){
						layer.msg('账户状态修改成功！',{icon: 1,time: 2000});
						oTable.fnUpdate(managerStatus, nRow,9, false);
						oTable.fnDraw();
					}else{
						layer.msg('账户状态未修改！',{icon: 2,time: 2000});
						return;
					}
			   });
            });
			
			//删除
            $('#table_common a.delete').live('click', function (e) {
                e.preventDefault();
				var nRow = $(this).parents('tr')[0];
				var aData = oTable.fnGetData(nRow);
				layer.confirm('确定删除该职员？',function(index){
					$.post(ajaxUrl,{managerId:aData['managerId'],ajax:'delManagers'},function(data){
						if(data){
							layer.msg('职员删除成功！',{icon: 1,time: 2000});
							oTable.fnDeleteRow(nRow);
						}else{
							layer.msg('职员删除失败！',{icon: 1,time: 2000});
							return;
						}
				   });					
				});
            });

            //取消按钮
			$('button[data-dismiss="modal"]').live('click', function (e) {
                e.preventDefault();
				$('#responsive').modal('hide');//关闭模态框
				validator.resetForm();//重置验证表单
				$('#responsive .form-group').removeClass('has-error');//去除遗留样式	
            });

            $('#table_common a.edit').live('click', function (e) {
                e.preventDefault();
				//$('#responsive form').attr('class','edit-form');//表单切换
				$('.modal-header h4')[0].innerHTML = '修改职员';//表头修改
				
                var nRow = $(this).parents('tr')[0];//取得当前行
				var aData = oTable.fnGetData(nRow);//取得数据
				var jqName = $('#responsive input[name="loginName"]').eq(0);
				jqName.val(aData['loginName']);//表头赋值
				jqName.attr('disabled',true);//禁止修改
				var jqAjax = $('#responsive input[name="noAjax"]').eq(0);
				jqAjax.val('editManagers');//切换提交地址
				var jqRole = $('#responsive input[name="managerId"]').eq(0);
				jqRole.val(aData['managerId']);//角色id赋值
				//$('#responsive button[type="submit"]').eq(0).addClass('edit')//标记编辑按钮
				//赋值
				$('#responsive input[name="managerName"]').eq(0).val(aData['managerName']);
				$('#responsive input[name="managerNo"]').eq(0).val(aData['managerNo']);
				$('#responsive input[name="managerId"]').eq(0).val(aData['managerId']);
				$('.fileinput img').attr('src',Upload+'/'+aData['managerPhoto'].replace(".","_m."));
				//checkbox
				$('#responsive input[type="checkbox"]').attr('checked',false);
				$('#responsive input[type="checkbox"]').parent('span').removeClass('checked');
				if(aData['workStatus']==1){
					$('#responsive input[name="workStatus"]').eq(0).attr('checked',true);
					$('#responsive input[name="workStatus"]').eq(0).parent('span').addClass('checked');
				}
				if(aData['managerStatus']==1){
					$('#responsive input[name="managerStatus"]').eq(0).attr('checked',true);
					$('#responsive input[name="managerStatus"]').eq(0).parent('span').addClass('checked');
				}
				
				if(aData['managerId']==1 || aData['managerId']==managerId){
					$('#responsive input[type="checkbox"]').attr('disabled',true);
					$('.checkbox-inline div').addClass('disabled');
				}else{
					$('#responsive input[type="checkbox"]').attr('disabled',false);
					$('.checkbox-inline div').removeClass('disabled');
				}
				
				//selected
				var jqOpts = $('option');
				for (var i = 0, iLen = jqOpts.length; i < iLen; i++) {
					//先清空
					jqOpts.eq(i).attr('selected',false);					
					if(aData['managerRoleId']==jqOpts.eq(i).val()){
						jqOpts.eq(i).attr('selected',true);						
					}
				} 
				
				//打开模态框
				$('#responsive').modal('show');
				$("#loginPwd").rules('remove','required');//移除验证规则
				$("#reLoginPwd").rules('remove','required');
            });
        }

    };

}();